package com.wwg.warehouse.data

import android.os.Parcelable
import androidx.room.*
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.wwg.warehouse.utils.fromJsonObject
import kotlinx.parcelize.Parcelize

/**
 * 使用邻接表来存储树
 */
@Parcelize
@TypeConverters(DBEntity.Converters::class)
@Entity(
    tableName = "bookmarks",
    primaryKeys = ["name", "path"]
)
data class DBEntity(
    var name: String,
    var path: String,

    var parentPath: String,
    var parentName: String,

    var content: Content? = null,

    /**
     * order < 0, 置顶；order > 0, 正常顺序
     */
    @ColumnInfo(name = "sortOrder")
    var order: Int = 0
): Parcelable {
    class Converters {
        private val gson = Gson()

        @TypeConverter
        fun readContentToString(content: Content?): String = gson.toJson(content)

        @TypeConverter
        fun stringToReadConfig(json: String) = gson.fromJsonObject<Content?>(json)
    }
}

@Parcelize
data class Content(
    @SerializedName("uid")
    val uid: String,
    @SerializedName("url")
    val url: String = "",
    @SerializedName("isFolder")
    val isFolder: Boolean,
    @SerializedName("date")
    val date: String,

    @SerializedName("state")
    val state: Int,
): Parcelable
